package com.wenx.mutiwebservices;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;

public class JettyToXfireWeb {

    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRouteDefinition(initRoute2());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }

    public static RouteDefinition initRoute2(){
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition
                .from("jetty:http://localhost:9080/camel")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("访问成功!");
                        String content="<?xml version=\"1.0\" encoding=\"utf-8\"?>  <soap:Envelope"
                                + " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                                + "<soap:Body> " +
                                "        <sayHello xmlns=\"http://test.xfire.com\"> " +
                                "            <id>123</id>  " +
                                "            <name>张三</name>  " +
                                "        </sayHello> " +
                                "    </soap:Body> "
                                + "</soap:Envelope>";
                        exchange.getOut().setBody(content);
                    }
                })
                .to("cxf:"
                        + "http://localhost:8089/services/HelloService" //service address
                        + "?"
                        + "wsdlURL=http://localhost:8089/services/HelloService?wsdl"    //wsdl url
                        + "&"
                        + "dataFormat=MESSAGE"
                                +         //dataformat type
                        "&" +
                        "endpointName=HelloServiceHttpPort"
//                        "&" +
//                        "serviceName=WeatherWebService"
                )
//                .process(new Processor() {
//                    @Override
//                    public void process(Exchange exchange) throws Exception {
//                        //强转成String可能会出现问题，要注意
//                        String body = exchange.getIn().getBody(String.class);
//                        System.out.println("body = " + body);
//                    }
//                })
                ;
        return routeDefinition;
    }
}
